package physics;

import java.awt.Shape;

import physics.shape.Circle;

/**
 * a forcefield is a place in the world where any movable obstacle inside it will receive a new force from
 * obstacles can move through an forcefield without collision but receive a new force from it depending on it location.
 * @author Thomas,Jannes
 *
 */
public class ForceField {
	private Shape shape;
	private Coordinate location; // center of the boundingbox
	private double forceMax; // the max amount of force the field can provide
	private double maxDistance; // the maximum distance from the center of the field
	
	public ForceField(Shape shape, int maxForce){
		this.shape = shape;
		this.location = new Coordinate(shape.getBounds().getCenterX(),shape.getBounds().getCenterY());
		this.forceMax = maxForce;
		Coordinate leftupper = new Coordinate(shape.getBounds().getX(),shape.getBounds().getY());
		this.maxDistance = Coordinate.getDistance(leftupper,location);
	}
	
	/**
	 * checks if a given coordinate is inside the forcefield
	 * @param coordinate
	 * 			|the Coordinate to check
	 * @return
	 * 			|true if the coordinate is inside the shape of the field, false otherwise
	 */
	public boolean inField(Coordinate coordinate){
		return shape.contains(coordinate.x, coordinate.y);//TODO
	}
	
	
	/**
	 * returns the force a point at a given coordinate receives from this forcefield
	 * @param coordinate
	 * 			|the Coordinate to check
	 * @return
	 * 			|the vector representing the force received by the field
	 */
	public Vector getForceVector(Coordinate coordinate){
		if (inField(coordinate)){
			double distance = Coordinate.getDistance(this.getCoordinate(), coordinate);
			double force = forceMax * (maxDistance/distance);
			double angle = Coordinate.getAngle(coordinate, getCoordinate());
			
			return new Vector(angle,force);
		}
		return new Vector(0,0);
	}

	private Coordinate getCoordinate() {
		return  new Coordinate(shape.getBounds().getCenterX(),shape.getBounds().getCenterY());
	}
	
	public static void main(String args[]){
		Shape circle = new Circle(new Coordinate(150,150), 20);
		ForceField f = new ForceField(circle, 200);
		System.out.println(f.getForceVector(new Coordinate(155,155)));
	}
	
}
